package cn.wolfcode.web.controller;


import cn.wolfcode.domain.ActivetyEmployee;
import cn.wolfcode.domain.Activity;
import cn.wolfcode.domain.ActivityItem;
import cn.wolfcode.mapper.ActivetyEmployeeMapper;
import cn.wolfcode.service.IActivityItemService;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IActivityService;
import cn.wolfcode.service.IChoiceService;
import cn.wolfcode.util.RequiredPermission;
import cn.wolfcode.util.UserContext;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ModelAttribute;

import java.util.List;

@Controller
@RequestMapping("/activityItem")
public class ActivityItemController {

    @Autowired
    private IActivityItemService activityItemService;
    @Autowired
    private IActivityService activityService;
    @Autowired
    private IChoiceService choiceService;


    @RequiredPermission(name="投票选项页面",expression="activityItem:list")
    @RequestMapping("/list")
    public String list(Long id,Model model){
        Activity activity = activityService.get(id);
        model.addAttribute("activity",activity);
        List<ActivityItem> activityItems = activityItemService.getByActId(id);
        model.addAttribute("activityItems",activityItems);
        return "activity/input";
    }


    @RequiredPermission(name="投票选项删除",expression="activityItem:delete")
    @RequestMapping("/delete")
    public String delete(Long id) {
        if (id != null) {
           activityItemService.delete(id);
        }
        return "redirect:/activityItem/list";
    }


    @RequiredPermission(name="投票选项新增/编辑",expression="activityItem:saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Long optionsRadios,Long actId) {
        choiceService.saveByAcItemId(optionsRadios);
        choiceService.updateByActId(actId);
        activityItemService.updateNumByItemId(optionsRadios);

        return "redirect:/choice/list?id="+actId;
    }


    @RequiredPermission(name="投票选项新增/编辑",expression="activityItem:saveOrUpdate")
    @RequestMapping("/input")
    public String input(Long id, Model model) {
        if (id != null) {
            model.addAttribute("activityItem",activityItemService.get(id));
        }
        return "activityItem/input";
    }
}
